package com.ghxxzx.dagl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghxxzx.dagl.entity.*;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.ClPathMapper;
import com.ghxxzx.dagl.mapper.MenueMapper;
import com.ghxxzx.dagl.mapper.RoleMenueMapper;
import com.ghxxzx.dagl.mapper.UserMapper;
import com.ghxxzx.dagl.service.impl.UserServiceImpl;
import com.ghxxzx.dagl.utils.PageResultUtils;
import com.ghxxzx.dagl.utils.PathUtils;
import com.ghxxzx.dagl.vo.*;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * desc:
 *
 * @author cx316
 * Created by cx316 on 2020-05-27.
 */
@Api(value = "用户管理")
@RestController
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    private PathUtils pathUtils;
    @Autowired
    private ClPathMapper clPathMapper;
    Logger log = LoggerFactory.getLogger(getClass());

    @RequestMapping("updatePath")
    @ApiOperation(value = "修改档案材料路劲", notes = "更新")
    public ResultEntity updatePath(String path) {
        try {
            path = URLDecoder.decode(path, "utf-8");
            if (!path.matches("^[A-z]:/(.+?)*$")) {
                throw new JyException("请输入正确的路劲");
            }
            ClPath clPath = clPathMapper.selectPath();
            clPath.setPath(path);
            pathUtils.setPath(path);
            clPathMapper.updateById(clPath);

            return ResultEntity.ok();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

//    @ApiOperation(value = "修改文书档案材料路劲", notes = "更新")
//    @ApiImplicitParam(name = "path", value = "路劲", required = true, paramType = "query", dataType = "path")
//    @RequestMapping("updateWsPath")
//    public ResultEntity updateWsPath(String path) {
//        try {
//            if (!path.matches("^[A-z]:/(.+?)*$")) {
//                throw new JyException("请输入正确的路劲");
//            }
//            ClPath clPath = clPathMapper.selectWsPath();
//            clPath.setPath(path);
//            clPathMapper.updateById(clPath);
//            return ResultEntity.ok();
//        } catch (JyException e) {
//            return ResultEntity.error(e.getMess());
//        } catch (Exception e) {
//            return ResultEntity.error("内部错误");
//        }
//    }


    @RequestMapping("getUser")
    public ResultEntity getUser(HttpSession httpSession) {
        try {
            User user = (User) httpSession.getAttribute("user");
            Integer id = null;
            if (user == null) {
                user = new User();
                user.setId(4);
                id = user.getId();
            } else {
                id = user.getId();
            }
            User user1 = userMapper.selectById(id);
            return ResultEntity.ok(user1);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @RequestMapping("addUser")
    public ResultEntity addUser(User user) {
        if (StringUtils.isEmpty(user.getAccount())) {
            return ResultEntity.error("账户不能为空");
        }
        if (StringUtils.isEmpty(user.getPhone())) {
            return ResultEntity.error("电话不能为空");
        }
        if (StringUtils.isEmpty(user.getName())) {
            return ResultEntity.error("姓名不能为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return ResultEntity.error("密码不能为空");
        }
        if (StringUtils.isEmpty(user.getDw())) {
            return ResultEntity.error("单位不能为空");
        }
        if (user.getRoleId() == null) {
            return ResultEntity.error("权限不能为空");
        }
        try {
            userService.addUser(user);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
        return ResultEntity.ok();
    }

    @ApiOperation(value = "用户更新", notes = "更新")
    @ApiImplicitParam(name = "user", value = "用户对象", required = true, paramType = "query", dataType = "user")
    @RequestMapping("updataUser")
    public ResultEntity updataUser(User user,HttpSession httpSession) {
        try {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("account",user.getAccount());
            userQueryWrapper.ne("id",user.getId());
            User selectOne = userMapper.selectOne(userQueryWrapper);
            if (selectOne!=null){
                return ResultEntity.error("该账户已重复");
            }
//            QueryWrapper<Rgsh> wrapper = new QueryWrapper<>();
            User user1 = (User) httpSession.getAttribute("user");
//            if (user.getRoleId().intValue()!=user1.getRoleId().intValue()){
//                if (user1.getRoleId()==1){
//
//                }else if (user.getRoleId()==2){
//
//                }else if (user.getRoleId()==3){
//
//                }
//            }
            if (user1.getId().intValue()==user.getId().intValue()){
                user1.setName(user.getName());
                user1.setPhone(user.getPhone());
                user1.setAccount(user.getAccount());
                user1.setDw(user.getDw());
                user1.setPassword(user.getPassword());
                user1.setRoleId(user.getRoleId());
            }

            return userService.updataUser(user);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @RequestMapping("listUserPage")
    public ResultEntity listUserPage(PageQuery pageQuery,String keyword) {
        Page page = userService.selectPage(pageQuery,keyword);
        PageResult pageResult = PageResultUtils.generatePageResult(page, pageQuery);
        return ResultEntity.ok(pageResult);
    }

    @RequestMapping("listShUser")
    public ResultEntity listShUser() {
        List<User> users = userService.selectAllShry();
        return ResultEntity.ok(users);
    }


    @RequestMapping("listRgShUser")
    public ResultEntity listRgShUser() {
        List<User> users = userService.listRgShUser();
        return ResultEntity.ok(users);
    }

    @RequestMapping("deleteUser")
    public ResultEntity deleteUser(Integer id) {
        userService.deleteUserById(id);
        return ResultEntity.ok();
    }

    //职务
    @ApiOperation(value = "新增Role", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "职务名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ids", value = "菜单id", required = true, paramType = "query", dataType = "int", allowMultiple = true)
    })
    @RequestMapping("addRole")
    public ResultEntity addRole(String name, @RequestParam(value = "ids[]") List<Integer> ids) {
        try {
            return userService.addRole(name, ids);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "更新用户回显", notes = "查询")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "query", dataType = "int")
    @RequestMapping("updataQueryUser")
    public ResultEntity updataQueryUser(Integer id) {
        try {
            return userService.updataQueryUser(id);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "修改职位回显", notes = "查询")
    @ApiImplicitParam(name = "id", value = "职务id", required = true, paramType = "query", dataType = "int")
    @RequestMapping("updateRoleQuery")
    public ResultEntity updateRoleQuery(Integer id) {
        try {
            return userService.updateRoleQuery(id);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "查询菜单", notes = "查询")
    @RequestMapping("queryMenueAll")
    public ResultEntity queryMenueAll() {
        try {
            return userService.queryMenueAll();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "查询所有职务", notes = "查询")
    @RequestMapping("queryRoleAll")
    public ResultEntity queryRoleAll() {
        try {
            return userService.queryRoleAll();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "职务更新", notes = "更新")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "role", value = "职务名称", required = true, paramType = "query", dataType = "role"),
//            @ApiImplicitParam(name = "ids", value = "菜单id", required = true, paramType = "query", dataType = "int", allowMultiple = true)
//    })
    @RequestMapping("updateRole")
    public ResultEntity updateRole(Role role, @RequestParam("ids[]") Integer[] ids) {
        try {
            return userService.updateRole(role, Lists.newArrayList(ids));
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "职务删除", notes = "删除")
    @RequestMapping("deleteRole")
    public ResultEntity deleteRole(Integer roleId) {
        try {
            userService.deleteRole(roleId);
            return ResultEntity.ok();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @RequestMapping("querRole")
    public ResultEntity querRole() {
        return userService.querRole();
    }
}
